const Model = require("./Model")
class User extends Model {
  // 登录
  static doLogin(username, password) {
    return new Promise((resolve, reject) => {
      const sql = "SELECT * FROM `user` WHERE username=? AND `password`=?"
      this.query(sql, [username, password]).then(resolve).catch(reject)
    })
  }
  //注册
  static doRegister(username, password, vip_expire_time, salt, nickname) {
    return new Promise((resolve, reject) => {
      const sql = "INSERT INTO user (username, password, vip_expire_time,salt,nickname) VALUES (?,?,?,?,?)"
      this.query(sql, [username, password, vip_expire_time, salt, nickname]).then(resolve).catch(reject)
    })
  }
  //查询单个用户
  static findOne(username) {
    return new Promise((resolve, reject) => {
      const sql = "SELECT id,username,password,salt,nickname, DATE_FORMAT(vip_expire_time, '%Y-%m-%d %H:%i:%s') AS vip_expire_time FROM `user` WHERE username=?"
      this.query(sql, [username]).then(resolve).catch(reject)
    })
  }

  //获取用户列表
  static findAll(keyword, currentPage, pageSize) {
    return new Promise((resolve, reject) => {
      // const sql = `SELECT * FROM user LIMIT ${pageNum} OFFSET ${currentPage}`
      const sql = `SELECT id,username,nickname,DATE_FORMAT(vip_expire_time, '%Y-%m-%d %H:%i:%s') AS vip_expire_time,DATE_FORMAT(crea_time, '%Y-%m-%d %H:%i:%s') AS crea_time FROM user
      WHERE username LIKE ?
      ORDER BY crea_time DESC
      LIMIT ? OFFSET ?;`
      this.query(sql, [`%${keyword}%`, pageSize, currentPage])
        .then(resolve)
        .catch(reject)
    })
  }
  //获取用户总数
  static count(keyword) {
    return new Promise((resolve, reject) => {
      const sql = "SELECT COUNT(*) AS count FROM `user` where username LIKE ?"
      this.query(sql, [`%${keyword}%`])
        .then((result) => {
          const count = result[0].count
          resolve(count)
        })
        .catch(reject)
    })
  }
  //修改会员到期时间
  static updateVipExpireTime(id, vipExpireTime) {
    return new Promise((resolve, reject) => {
      const sql = "UPDATE user SET vip_expire_time = ? WHERE id = ?"
      this.query(sql, [vipExpireTime, id]).then(resolve).catch(reject)
    })
  }

  //修改会员到期时间和用户密码
  static updateUserPasswordAdmin(id, vipExpireTime, password, salt) {
    return new Promise((resolve, reject) => {
      const sql = "UPDATE user SET vip_expire_time = ?,password = ?,salt=? WHERE id = ?"
      this.query(sql, [vipExpireTime, password, salt, id]).then(resolve).catch(reject)
    })
  }

  //修改密码
  static updateUserPassword(id, password) {
    return new Promise((resolve, reject) => {
      const sql = "UPDATE user SET password = ? WHERE id = ?"
      this.query(sql, [password, id]).then(resolve).catch(reject)
    })
  }
  // 修改用户token
  static updateUserToken(id, token) {
    return new Promise((resolve, reject) => {
      const sql = "UPDATE user SET token = ? WHERE id = ?"
      this.query(sql, [token, id]).then(resolve).catch(reject)
    })
  }
  //删除用户
  static deleteUser(id) {
    return new Promise((resolve, reject) => {
      const sql = "DELETE FROM user WHERE id = ?"
      this.query(sql, [id]).then(resolve).catch(reject)
    })
  }
  // 修改密码
  static editPassWord(username, newPwd) {
    return new Promise((resolve, reject) => {
      const sql = "UPDATE user SET password = ? WHERE `username` =?"
      this.query(sql, [newPwd, username]).then(resolve).catch(reject)
    })
  }

  // 最后登录时间
  static lastLoginTime() {
    return new Promise((resolve, reject) => {
      const sql = "SELECT `time` FROM log WHERE handle='登录' ORDER BY `time` DESC LIMIT 1"
      this.query(sql).then(resolve).catch(reject)
    })
  }
}

module.exports = User
